package com.google.code.xconf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;

public class PropertyGroup extends Property {

    protected Collection<Property> nodes = new ArrayList<>();

    public PropertyGroup() {
    }

    public PropertyGroup(String name) {
        super(name);
    }

    public Collection<Property> getProperties() {
        List<Property> result = new ArrayList<>();
        for (Property node : nodes) {
            if (node.getClass() == Property.class) {
                result.add(node);
            }
        }
        return result;
    }

    public Collection<Property> getProperties(String identifier) {
        List<Property> result = new ArrayList<>();
        for (Property node : getProperties()) {
            if (node.getName().equals(identifier)) {
                result.add(node);
            }
        }
        return result;
    }

    public Property getFirstProperty(String identifier) {
        List<Property> matches = (List) getProperties(identifier);
        if (matches.isEmpty()) {
            return null;
        }
        //noinspection unchecked
        return matches.get(0);
    }

    public Property getLastProperty(String identifier) {
        List<Property> matches = (List) getProperties(identifier);
        if (matches.isEmpty()) {
            return null;
        }
        return matches.get(matches.size() - 1);
    }

    public boolean containsProperty(String identifier) {
        for (Property node : getProperties()) {
            if (node.getName().equals(identifier)) {
                return true;
            }
        }
        return false;
    }

    public void addNode(Property node) {
        nodes.add(node);
    }

    public void addNodes(Collection<Property> nodes) {
        this.nodes.addAll(nodes);
    }

    public Collection<Property> getNodes() {
        return nodes;
    }

    public Collection<PropertyGroup> getPropertyGroups() {
        List<PropertyGroup> result = new ArrayList<>();
        for (Property node : nodes) {
            if (node instanceof PropertyGroup) {
                result.add((PropertyGroup) node);
            }
        }
        return result;
    }

    public boolean containsPropertyGroup(String identifier) {
        for (Property node : getPropertyGroups()) {
            if (node.getName().equals(identifier)) {
                return true;
            }
        }
        return false;
    }

    public Collection<PropertyGroup> getPropertyGroups(String key) {
        List<PropertyGroup> result = new ArrayList<>();
        for (Property node : nodes) {
            if (node instanceof PropertyGroup) {
                if (node.getName().equals(key)) {
                    result.add((PropertyGroup) node);
                }
            }
        }
        return result;
    }

    @Override
    public String toString() {
        return "PropertyGroup{" +
                "name=" + name + "," +
                "nodes=" + nodes +
                '}';
    }
}
